<?php
namespace modules\post;

class storage
{
    static function schemas(): array
    {
        return [
            'post' => [
                'id' => 'identity',
                'author' => '@user/auth',
                'title' => 'string/256',
                'body' => 'string',
            ],
        ];
    }

    static function list($q, int $page)
    {
        return $q->table('post')
            ->field('id', 'title', 'author.login author')
            ->order('>id')
            ->select(10, ($page-1)*10);
    }

    static function fetch($q, int $id)
    {
        return $q->table('post', $id)
            ->field('id', 'title', 'body', 'author.login author')
            ->select(0);
    }

    static function add($q, $title, $body, $author)
    {
        return $q->table('post')
            ->field('title', 'body', 'author')
            ->insert($title, $body, $author);
    }

    static function edit($q, int $id, $title, $body)
    {
        return $q->table('post', $id)
            ->field('title', 'body')
            ->update($title, $body);
    }

    static function delete($q, int $id)
    {
        return $q->table('post', $id)
            ->delete();
    }
}

